'use strict';

app.controller('reqNoticeCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$modal', 'lodash','$location','$anchorScroll','$timeout', function ($scope, $http, $stateParams, $rootScope, $modal, lodash,$location,$anchorScroll,$timeout) {
	$scope.info={
        notice: "",
    };
    $rootScope.initBreadcrumb($scope.breadcrumb);
	$rootScope.initHeaderElements($scope.settings);
    var backUrl = {
        show: true,
        label: "返回",
        func: function () {
            $rootScope.backHistory();
        }
    }
    $scope.settings.btnEle.push(backUrl);
    getReqDetails();
    getReqNotices();
    var oldContent="";

    // 获取需求名称
	function getReqDetails() {
        var url = matrix_product_url +"/brdRequirement/getBrdRequirements?reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.reqDetails = result.data[0];
                    $scope.breadcrumb.push('产品设计', $scope.reqDetails ? $scope.reqDetails.reqName : "");
                } else {
                    $rootScope.toasterErrorInfo("获取需求数据错误！");    
                }
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

    // 获取公告数据
    function getReqNotices() {
        $rootScope.showPanelLoading();
        var url = matrix_product_url+"/brdRequirement/getReqNotices?reqId="+$stateParams.reqId;
    	$http.get(url).success(function(result){
    		if(result.code == "0") {
    			$scope.info.notice = result.data[0];
                if(result.data[0]){
                    oldContent=result.data[0].content;
                }
    		}else{
    			$rootScope.toasterErrorInfo(result.desc);
            }
            $rootScope.hidePanelLoading();
    	})
    }


    $scope.saveBlurData=function(value){
    	if(!value || oldContent == value){
    		return false;
    	}
    	var url=matrix_product_url+"/brdRequirement/saveReqNotice"
    	var postParams={
    		"id"	  : $scope.info.notice ? ($scope.info.notice.id || "") : "",
    		"number"  : $scope.info.notice ? ($scope.info.notice.number || "") : "",
		    "reqId"   : $stateParams.reqId,
		    "content" : value
    	}

    	$http({
            method  : "post",
            url     : url,
            data    : JSON.stringify(postParams),
            headers : {"Content-Type": "application/json"}
        }).success(function(result) {
            if(result.code == "0") {
            	$scope.info.notice=result.data;
                oldContent=result.data.content;
                $rootScope.showMessage("success", "成功", (!$scope.info.notice.id ? "创建成功！" : "修改成功！"));
            } else {
                $rootScope.showMessage("error", "失败", result.desc);
            }
        });
    }
}])